A Piece of Java: Introduction to Programming by Kim Faith

A Piece of Java: Introduction to Programming by Kim Faith

Author:Kim, Faith [Kim, Faith]
Language: eng
Format: epub
Published: 2021-02-02T00:00:00+00:00


Code 9.8: Writing the code the method must execute inside its body

We need to also return the added value at the end of the method’s body:

Code 9.9: Adding the return statement

The return is a Java keyword used to return data at the end of the method. Note again that code in Java is run line by line, so the return statement must always go at the end of a method.

For example, we cannot do this:

Code 9.10: Erroneous code. You cannot return earlier when there’s more code to process afterwards.

Code 9.10 will throw us a compilation error because Java sees that there is another line of code it must process after the return statement.

Also note that a method cannot return more than one data. It can only return one.

But now we’re pretty much done with the add() method. Since the original code also prints our operation, I will create an additional print statement:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.